package cn.wolfcode.interceptor;

import cn.wolfcode.anno.RequiredLogin;
import cn.wolfcode.domain.User;
import cn.wolfcode.redis.IUserInfoRedisService;
import cn.wolfcode.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class
CheckLoginInterceptor implements HandlerInterceptor {
  /*  @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String cookie = request.getHeader("Cookie");
        String[] cookies = cookie.split("=");
        String token = cookies[3];
        User user = userInfoRedisService.getUserByToken(token);

        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        //判断方法是否贴了RequiredLogin注解,贴了就需要登录
        HandlerMethod hm = (HandlerMethod) handler;
        if (hm.hasMethodAnnotation(RequiredLogin.class)) {
            if (user == null) {
                //返回json格式,没有登录的提示
                response.setContentType("application/json;charset=utf-8");
                response.getWriter().write(JSON.toJSONString(JsonResult.noLogin()));
                return false;
            }
        }
        return true;
    }*/
}
